package com.oscar.ehome.view.konwledge;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.fragment.BaseFragment;
import com.oscar.ehome.base.fragment.adapter.CustomFragmentPagerAdapter;
import com.oscar.ehome.base.fragment.adapter.NoPreloadViewPager;
import com.oscar.ehome.presenter.BasePresenter;
import com.oscar.ehome.utils.basethem.TintManager;
import com.oscar.ehome.utils.system.SystemConfigFactory;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/9/20
 * 律师团队问答页
 */

public class KnowPrombleFragment extends BaseFragment implements NoPreloadViewPager.OnPageChangeListener, CompoundButton.OnCheckedChangeListener {

    public View view;
    Unbinder unbinder;

    /**
     *
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     *
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    /**
     *
     */
    @BindView(R.id.konwlepromble_no)
    RadioButton konwleprombleNo;
    /**
     *
     */
    @BindView(R.id.konwlepromble_ok)
    RadioButton konwleprombleOk;
    /**
     *
     */
    @BindView(R.id.konwlepromble_group)
    RadioGroup konwleprombleGroup;
    /**
     *
     */
    @BindView(R.id.konwlepromble_frg)
    NoPreloadViewPager konwleprombleFrg;

    private List<Fragment> fragmentList = new ArrayList<Fragment>();
    CustomFragmentPagerAdapter adapter = null;
    private int currIndex = 0;
    private String color = "";


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_knowpromble, null);
        unbinder = ButterKnife.bind(this, view);
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(getActivity()).getSystemConfig().getColor()));
        initView();
        setColor();
        onClicks();
        initPager();
        return view;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }


    /**
     * 初始化
     */
    public void initView() {
        titleTopbar.setText(getString(R.string.konwle_prombles));
    }

    /**
     * 设置颜色
     */
    public void setColor() {
        if (SystemConfigFactory.getInstance(getActivity()).getSystemConfig().getColor() == null ||
                "".equals(SystemConfigFactory.getInstance(getActivity()).getSystemConfig().getColor())) {
            color = "#00a0e9";
            SystemConfigFactory.getInstance(getActivity()).getSystemConfig().setColor(color);
        }
        konwleprombleNo.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(getActivity()).getSystemConfig().getColor()));
        konwleprombleOk.setTextColor(Color.parseColor("#999999"));
        TintManager.setCompoundTint(konwleprombleNo, Color.parseColor(SystemConfigFactory.getInstance(getActivity()).getSystemConfig().getColor()), false);
        TintManager.setCompoundTint(konwleprombleOk, Color.parseColor("#999999"), false);

    }

    /**
     * 点击事件
     */
    public void onClicks() {
        konwleprombleNo.setOnCheckedChangeListener(this);
        konwleprombleOk.setOnCheckedChangeListener(this);
        konwleprombleFrg.setOnPageChangeListener(this);
    }

    /**
     * 其他人员加载页面
     */
    public void initPager() {
        fragmentList.add(new KnownoPrombleFragment());
        fragmentList.add(new KnowokPrombleFragment());
        adapter = new CustomFragmentPagerAdapter(getFragmentManager(), fragmentList);
        konwleprombleFrg.setAdapter(adapter);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (konwleprombleNo.isChecked()) {
            TintManager.setCompoundTint(konwleprombleNo, Color.parseColor(SystemConfigFactory.getInstance(getActivity()).getSystemConfig().getColor()), false);
            konwleprombleNo.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(getActivity()).getSystemConfig().getColor()));

            TintManager.setCompoundTint(konwleprombleOk, Color.parseColor("#999999"), false);
            konwleprombleOk.setTextColor(Color.parseColor("#999999"));
            konwleprombleFrg.setCurrentItem(0);
        } else if (konwleprombleOk.isChecked()) {
            TintManager.setCompoundTint(konwleprombleOk, Color.parseColor(SystemConfigFactory.getInstance(getActivity()).getSystemConfig().getColor()), false);
            konwleprombleOk.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(getActivity()).getSystemConfig().getColor()));

            TintManager.setCompoundTint(konwleprombleNo, Color.parseColor("#999999"), false);
            konwleprombleNo.setTextColor(Color.parseColor("#999999"));
            konwleprombleFrg.setCurrentItem(1);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                if (currIndex == 1) {// 从页卡1跳转转到页卡2
                    konwleprombleNo.setChecked(true);
                    konwleprombleOk.setChecked(false);
                } else if (currIndex == 2) {// 从页卡1跳转转到页卡3
                    konwleprombleNo.setChecked(true);
                    konwleprombleOk.setChecked(false);
                }
                break;
            case 1:
                if (currIndex == 0) {// 从页卡1跳转转到页卡2
                    konwleprombleNo.setChecked(false);
                    konwleprombleOk.setChecked(true);
                } else if (currIndex == 2) {// 从页卡1跳转转到页卡3
                    konwleprombleNo.setChecked(false);
                    konwleprombleOk.setChecked(true);
                }
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
